/**
 * LoginPanel.java
 */
package edu.gatech.coc.betameisters.userinterfaces;

/**
 * This is the UserInterface class. It doesn't do much now except for check for logins
 * @author Henry
 * @version 1.0
 */

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import edu.gatech.coc.betameisters.people.Person;
import edu.gatech.coc.betameisters.utilities.Database;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class LoginPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6145879112401157947L;

	/** field for username. */
	private JTextField userField;

	/** field for password. */
	private JTextField passField;

	/** fields for the buttons. */
	private JButton loginButton, exitButton;

	/** number of times user has tried to log in. */
	private int loginAttempts;

	/** number of times user may try to log in. */
	private final int MAX_ATTEMPTS = 3;

	/**
	 * Holding in previous username entered
	 */
	private String memUsername = "";
	
	/**
	 * necessary to be an instance variable so the actionlistener can change it.
	 */
	private Person returnValue = null;

	/** field for error messages. */
	private JLabel alert;

	/** action listener needs to be able to see this. */
	private boolean done;
	
	/**
	 * Field exit to inform us to end program (nicely)
	 */
	private boolean exit;

	/**
	 * Create the panel.
	 */
	public LoginPanel() {
		exit = false;
		setLayout(null);
		
		JLabel lblUsername = new JLabel("UserName: ");
		lblUsername.setBounds(141, 107, 84, 14);
		add(lblUsername);

		userField = new JTextField();
		userField.setBounds(217, 104, 86, 20);
		add(userField);
		userField.setColumns(10);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(141, 132, 64, 14);
		add(lblPassword);

		passField = new JPasswordField();
		//passField = new JTextField();
		((JPasswordField) passField).setEchoChar('*');
		passField.setBounds(217, 129, 86, 20);
		add(passField);
		passField.setColumns(10);

		alert = new JLabel("");
		alert.setBounds(156, 207, 115, 42);
		add(alert);

		loginButton = new JButton("Login");
		loginButton.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				login();
			}

			private Person login() {
				Person toLoad;
				
				if(memUsername.equals(userField.getText())){
					loginAttempts += 1;
				}
				else{
					memUsername = userField.getText();
					loginAttempts = 0;
				}
				
				if (loginAttempts >= MAX_ATTEMPTS) {
					alert.setText("Too many login attempts");
					done = true;
					returnValue = null;
					toLoad = Database.loadPatient(userField.getText());
					if(toLoad == null){
						alert.setText("Account does not exist in system");
					}
					else if(toLoad.getUserID().equals(memUsername)){
						toLoad.setSuspened(true);
						alert.setText("This account has been suspended.\n Please contact our System Administrator.");
					}
					else {
						toLoad = Database.loadNurse(userField.getText());
						if(toLoad.getUserID().equals(memUsername)){
							toLoad.setSuspened(true);
							alert.setText("This account has been suspended.\n Please contact our System Administrator.");
						}
						else{
							toLoad = Database.loadDoctor(userField.getText());
							if(toLoad.getUserID().equals(memUsername)){
								toLoad.setSuspened(true);
								alert.setText("This account has been suspended.\n Please contact our System Administrator.");
							}
						}
						toLoad = Database.loadSystemAdmin(userField.getText());
						if(toLoad != null){
							toLoad.setSuspened(true);
							alert.setText("This account has been suspended.\n Please contact our System Administrator.");
						}
					}
				}
				else{
				toLoad = Database.loadPatient(userField.getText());
				if (toLoad != null
						&& toLoad.getUserPass().equals(passField.getText())) {
					done = true;
					if(!toLoad.isSuspened()){
						returnValue = toLoad;
						alert.setText("Welcome, " + userField.getText());
					}
					else{
						toLoad = null;
						alert.setText("This account has been suspended.\n Please contact our System Administrator.");
					}
				}
				toLoad = Database.loadNurse(userField.getText());
				if (toLoad != null
						&& toLoad.getUserPass().equals(passField.getText())) {
					done = true;
					if(!toLoad.isSuspened()){
						returnValue = toLoad;
						alert.setText("Welcome, " + userField.getText());
					}
					else{
						toLoad = null;
						alert.setText("This account has been suspended.\n Please contact our System Administrator.");
					}
				}
				toLoad = Database.loadDoctor(userField.getText());
				if (toLoad != null
						&& toLoad.getUserPass().equals(passField.getText())) {
					if(!toLoad.isSuspened()){
						returnValue = toLoad;
						alert.setText("Welcome, " + userField.getText());
					}
					else{
						toLoad = null;
						alert.setText("This account has been suspended.\n Please contact our System Administrator.");
					}
				}
				toLoad = Database.loadSystemAdmin(userField.getText());
				if (toLoad != null
						&& toLoad.getUserPass().equals(passField.getText())) {
					if(!toLoad.isSuspened()){
						returnValue = toLoad;
						alert.setText("Welcome, " + userField.getText());
					}
					else{
						toLoad = null;
						alert.setText("This account has been suspended.\n Please contact our System Administrator.");
					}
				}
				if (done == false)// it must have been the wrong username if
									// we're not done
				{
					alert.setText("Wrong Username and Password combination");
				}
				}
				return toLoad;
			}
		});
		loginButton.setBounds(126, 173, 89, 23);
		add(loginButton);

		exitButton = new JButton("Exit");
		exitButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				done = true;
				returnValue = null;
				exit = true;
			}
		});
		exitButton.setBounds(217, 173, 89, 23);
		add(exitButton);
	}
	
	public Person getLoggedIn(){
		return returnValue;
	}

	/**
	 * @param exit the exit to set
	 */
	public void setExit(boolean exit) {
		this.exit = exit;
	}

	/**
	 * @return the exit
	 */
	public boolean isExit() {
		return exit;
	}
}
